package com.jh3502.vivo.function.messagedetail;

import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author harisucici
 * @version 2018/11/28 08:19
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class MessageDetailActivity extends BaseActivity<MessageDetailPresenter.MessageDetailView, MessageDetailPresenter> implements MessageDetailPresenter.MessageDetailView {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.msg_detail_title)
    TextView msgDetailTitle;
    @BindView(R.id.msg_detail_point)
    ImageView msgDetailPoint;
    @BindView(R.id.msg_detail_status)
    TextView msgDetailStatus;
    @BindView(R.id.msg_detail_view)
    Button msgDetailView;
    @BindView(R.id.msg_detail_num)
    TextView msgDetailNum;
    @BindView(R.id.msg_detail_date)
    TextView msgDetailDate;
    @BindView(R.id.msg_detail_dep)
    TextView msgDetailDep;
    @BindView(R.id.msg_detail_recycler)
    MyRecyclerView msgDetailRecycler;
    @BindView(R.id.msg_detail_head)
    ConstraintLayout msgDetailHead;
    private MessageDetailAdapter mAdapter;
    private List<Map<String, Object>> dataList = new ArrayList<>(2);
    private String id, busId, type;

    @Override
    public MessageDetailPresenter initPresenter() {
        return new MessageDetailPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_message_detail;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("消息");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mPresenter.setMessageDetailView(this);
        mAdapter = new MessageDetailAdapter(this, dataList);
        msgDetailRecycler.setAdapter(mAdapter);
        type = getIntent().getExtras().getString("type");
        if ("3".equals(type)) {
            msgDetailHead.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {
        id = getIntent().getExtras().getString("id");
        busId = getIntent().getExtras().getString("busId");

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {
        Map<String, Object> body = new HashMap<>(2);
        body.put("id", id);
        body.put("busId", busId);
        mPresenter.toFindMsgRecords(body);
    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);
    }

    @Override
    public void findMsgRecordsSuccess(Map<String, Object> data) {
        dataList.clear();
        dataList.addAll(Utils.getInstance().jsa2list((String) data.get("records")));
        mAdapter.notifyDataSetChanged();
        //0：出差 1：报销
        String type = (String) data.get("type");
        if (type == null) {
            return;
        }
        if ("0".equals(type)) {
            msgDetailTitle.setText(data.get("destination") + "的出差申请");
        } else if ("1".equals(type)) {
            msgDetailTitle.setText(data.get("destination") + "的报销申请");
        }
        //0:审批中 1:已完成 2:被拒绝
        String state = (String) data.get("state");
        if ("0".equals(state)) {
            msgDetailStatus.setText("审批中");
            Glide.with(this).load(R.drawable.point_blue).into(msgDetailPoint);
            msgDetailStatus.setTextColor(Color.parseColor("#FF3389FF"));
        } else if ("1".equals(type)) {
            msgDetailStatus.setText("已批准");
            Glide.with(this).load(R.drawable.point_green).into(msgDetailPoint);
            msgDetailStatus.setTextColor(Color.parseColor("#FF0EC053"));
        } else if ("2".equals(type)) {
            msgDetailStatus.setText("审批被拒绝");
            Glide.with(this).load(R.drawable.point_red).into(msgDetailPoint);
            msgDetailStatus.setTextColor(Color.parseColor("#FFF36C60"));
        }
        msgDetailNum.setText((String) data.get("business_no"));
        msgDetailDate.setText((String) data.get("createdate"));
        msgDetailDep.setText((String) data.get("dept_name"));

    }

}
